/*
 * @Author: LCL 
 * @Date: 2019-03-08 20:36:30
 * @Desc: 楼梯管理器
*/

import { LaddGameUtil } from "../Public/LaddGameUtil";
import LadderItemControl from "./LadderItemControl";
import LadderData from "./LadderData";
import { PlayState } from "./LadderItemModel";

const {ccclass, property} = cc._decorator;

const fristLadY  : number = -365;
const ladStartY  : number = 1050;
const ladW       : number = 143;
const ladH       : number = 82;

@ccclass
export default class LadderControl extends cc.Component {

    @property(cc.Node)
    ladderParent: cc.Node = null;

    @property(cc.Prefab)
    ladderPrefab: cc.Prefab = null;

    @property(cc.SpriteFrame)
    normalSpri: cc.SpriteFrame = null;

    /**别的台阶 */
    @property(cc.SpriteFrame)
    otherSpris:Array<cc.SpriteFrame> = [];

    /**别的台阶午评 */
    @property(cc.SpriteFrame)
    otherIconSpris:Array<cc.SpriteFrame> = [];


    laddPool : cc.NodePool = null;

    lastIndex : number = 0;// 最后一层楼梯的索引
    topIndex : number = 0;// 最前面一层楼梯的索引
    formalItems : Array<cc.Node> = [] ;//正式楼梯数组
    otherItems : Array<cc.Node> = [] ;//装饰楼梯数组

    // fristLadY  : number = -365;
    // ladStartY  : number = 1050;
    // ladW       : number = 143;
    // ladH       : number = 82;

    timeCount = 1000;//起始1000ms
    private destroyTimer = null


    onLoad () {
        this.createLaddPool();
    }

    start () {
        this.initData();
    }


    public reset(){
        for(let i=0; i< this.formalItems.length; i++){
            let curNode = this.formalItems[i];
            curNode.removeFromParent();
            this.laddPool.put(curNode);
            
        }
        this.formalItems = [];

        for(let i=0; i< this.otherItems.length; i++){
            let curNode = this.otherItems[i];
            curNode.removeFromParent();
            this.laddPool.put(curNode);
        }
        this.otherItems = [];

        this.lastIndex = 0;
        this.topIndex = 0;
        this.timeCount = 1000;
        this.initData();
    }

    initData(){
        for(let i=0; i<7;i++){
            this.addLastFormalLadder();
        }
    }

    startRemoveTopLadd(){
        if(this.timeCount <0 || LadderData.getInstance().playState == PlayState.PLAY_END){
            return;
        }
        if(this.timeCount>2){
            this.timeCount = this.timeCount-5;
        }
        clearTimeout(this.destroyTimer );
        this.destroyTimer = setTimeout(() => {
            
           this.deleteTopFormalLadder(this.topIndex);
           this.deleteTopOtherLadder(this.topIndex);
           this.topIndex++;

           this.startRemoveTopLadd();
        }, this.timeCount)
    }

    //创建楼梯对象池
    createLaddPool () {
        this.laddPool = new cc.NodePool();
        let initCount = 10;
        for(let i=0; i<initCount; i++){
            let ladder = cc.instantiate(this.ladderPrefab);
            this.laddPool.put(ladder);
        }
    }

    //创建节点
    createLaddNode():cc.Node{
        let ladderItem = null;
        if (this.laddPool.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
            ladderItem = this.laddPool.get();
        } else { // 如果没有空闲对象，也就是对象池中备用对象不够时，我们就用 cc.instantiate 重新创建
            ladderItem = cc.instantiate(this.ladderPrefab);
        }
        return ladderItem;
    }

    //添加后面正式楼梯
    addLastFormalLadder(){

        let index = this.lastIndex;
        let randType = LaddGameUtil.getRandomInt(0,1);
        if(randType == 0){
            randType = -1;
        }

        let ladderItem = this.createLaddNode();
        ladderItem.parent = this.ladderParent; // 将生成的敌人加入节点树
        ladderItem.getComponent(LadderItemControl).initDataView(index,randType,1,this.normalSpri,null); //接下来就可以调用 enemy 身上的脚本进行初始化
        this.formalItems.push(ladderItem);
        this.setLaddPosition(ladderItem);

        this.addLastOtherLadder(index);

        this.lastIndex++;
    }

    //销毁前面正式楼梯
    deleteTopFormalLadder(index:number){

        for(let i=0; i< this.formalItems.length; i++){
            let curNode = this.formalItems[i];
            if(curNode.getComponent(LadderItemControl).index == index){
                curNode.runAction( cc.moveTo( 0.2 ,new cc.Vec2(curNode.position.x,curNode.position.y-350)));
                setTimeout(() => {
                    curNode.removeFromParent();
                    this.laddPool.put(curNode);
                    this.formalItems.shift();
                 }, 200);
            }
            
            if(LadderData.getInstance().playerCtr.curIndex == index){
                LadderData.getInstance().playerCtr.gameOver();
                return;
            }
        }
    }

    //获取最先正式的楼梯  也是 最下面的
    getTopLadder(index:number) : cc.Node{
        for(let i=0; i< this.formalItems.length; i++){
            let curNode = this.formalItems[i];
            if(curNode.getComponent(LadderItemControl).index == index){
               return curNode;
            }
        }
    }


    //添加后面装饰楼梯
    addLastOtherLadder(index : number){
        let randType = LaddGameUtil.getRandomInt(0,4)-2;
        if(randType == 0){
           return;
        }
        let randIcon = LaddGameUtil.getRandomInt(0,4);
        let ladderItem = this.createLaddNode();
        ladderItem.parent = this.ladderParent; // 将生成的敌人加入节点树
        ladderItem.getComponent(LadderItemControl).initDataView(index,randType,2,this.otherSpris[randIcon],this.otherIconSpris[randIcon]); //接下来就可以调用 enemy 身上的脚本进行初始化
        this.otherItems.push(ladderItem);
        this.setLaddPosition(ladderItem);
    }

    //销毁前面装饰楼梯
    deleteTopOtherLadder(index:number){
        for(let i=0; i< this.otherItems.length; i++){
            let curNode = this.otherItems[i];
            if(curNode.getComponent(LadderItemControl).index == index){
                curNode.runAction( cc.moveTo( 0.2 ,new cc.Vec2(curNode.position.x,curNode.position.y-350)));
                setTimeout(() => {
                    curNode.removeFromParent();
                    this.laddPool.put(curNode);
                    this.otherItems.shift();
                 }, 200);
            }
        }
    }

     //获取最先装饰的楼梯  也是 最下面的
     getTopOtherLadder(index:number) : cc.Node{
        for(let i=0; i< this.otherItems.length; i++){
            let curNode = this.otherItems[i];
            if(curNode.getComponent(LadderItemControl).index == index){
               return curNode;
            }
        }
    }


    setLaddPosition(curNode:cc.Node){
        let curLadCtr = curNode.getComponent(LadderItemControl);
        if(curLadCtr.index == 0){
            if(curLadCtr.role == 1){
                curLadCtr.endP = new cc.Vec2(0,fristLadY);
            }
            else{
                curLadCtr.endP = new cc.Vec2(143*curLadCtr.type,fristLadY);
            }
            curNode.position = curLadCtr.endP;
        }
        else{
            //根据上层楼梯来确定位置
            let  lastLadCtr : LadderItemControl = null;
            for(let i=0; i< this.formalItems.length; i++){
                let itemCtr = this.formalItems[i].getComponent(LadderItemControl);
                console.log("lastaaaaaaaaaaaa:"+itemCtr.endP  +" index:"+itemCtr.index);
                if(itemCtr.index == curLadCtr.index-1){
                    lastLadCtr = itemCtr;
                    break;
                }
            }
            console.log("orherlast==========:"+lastLadCtr.endP);
            if(lastLadCtr){
                if(curLadCtr.role == 1){
                    let endP:cc.Vec2 = new cc.Vec2(lastLadCtr.endP.x+ladW/2*curLadCtr.type,fristLadY+ladH*curLadCtr.index);
                    curLadCtr.endP = endP;
                    if(curLadCtr.index>6){
                        curNode.position = new cc.Vec2(endP.x, LadderData.getInstance().playerCtr.node.position.y+ladStartY);
                        curNode.runAction( cc.moveTo( 0.2 ,endP))
                    }
                    else{
                        curNode.position = endP;
                    }
                    console.log("last: "+lastLadCtr.index + " cur:" +curLadCtr.index +" type:"+curLadCtr.type);
                    console.log("last:"+lastLadCtr.endP +" cur:"+endP);
                }
                else{
                    for(let i=0; i< this.formalItems.length; i++){
                        let itemCtr = this.formalItems[i].getComponent(LadderItemControl);
                        console.log("otherlastaaaaaaaaaaaa:"+itemCtr.endP   +" index:"+itemCtr.index);
                        if(itemCtr.index == curLadCtr.index){
                            lastLadCtr = itemCtr;
                            break;
                        }
                    }
                    console.log("orherlast==========:"+lastLadCtr.endP);
                    let endP:cc.Vec2 = new cc.Vec2(lastLadCtr.endP.x+ladW*curLadCtr.type,fristLadY+ladH*curLadCtr.index);
                    curLadCtr.endP = endP;
                    if(curLadCtr.index>6){
                        curNode.position = new cc.Vec2(endP.x, LadderData.getInstance().playerCtr.node.position.y+ladStartY);
                        curNode.runAction( cc.moveTo( 0.2 ,endP))
                    }
                    else{
                        curNode.position = endP
                    }
                    console.log("orherlast: "+lastLadCtr.index + " cur:" +curLadCtr.index +" type:"+curLadCtr.type);
                    console.log("otherlast:"+lastLadCtr.endP +" cur:"+endP);
                }
                
                
            }

            

           
        }
    }





    // update (dt) {}
}


/*
public void TEXT1() 
{
	int textNum = 1;
	if (textNum == 1) 
	{
		textNum = 2;
    }
}
public void TEXT2()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
public void TEXT3()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT4()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT5()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT6()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT8()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT9()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT10()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT11()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT12()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT13()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT14()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT15()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT16()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}

public void TEXT17()
{
    int textNum = 1;
    if (textNum == 1)
    {
        textNum = 2;
    }
}
*/
